<%@Page Language="VB"%>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.Xpath" %>
<%@Import Namespace="System.Xml.Xsl" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Different ways to use the XSLTransform object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Different ways to use the XSLTransform object</span><hr />
<!--------------------------------------------------------------------------->

<div id="outError" runat="server" /><p />

Using XML document file:
<b><span id="outXMLURL" runat="server" /></b><br />
Loaded XSL stylesheet file:
<b><span id="outXSLURL" runat="server" /></b><p />
Output from transformation is:
<div id="outResults" runat="server" /><p />
Transformed to disk file:
<b><span id="outFile" runat="server" /></b>

<script language="vb" runat="server">

Sub Page_Load()

   'create virtual path to booklist.xml and authors.xsl files (in same folder as ASPX page)
   Dim strCurrentPath As String = Request.Url.ToString()
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "/")) & "booklist.xml"
   Dim strXSLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "/")) & "authors.xsl"
   Dim strOutPath As String = Left(Request.PhysicalPath, InStrRev(Request.PhysicalPath, "\")) & "authors.xml"

   Try

      'create a new XslTransform object to do the transformation
      Dim objTransform As New XslTransform()

      'load the XSL stylesheet into the XSLTransform object
      objTransform.Load(strXSLPath)
      outXSLURL.InnerHtml = "<a href=""" & strXSLPath & """>" & strXSLPath & "</a>"

      'create a new XmlTextReader object to fetch XML document
      Dim objXTReader As New XmlTextReader(strXMLPath)
      outXMLURL.InnerHtml = "<a href=""" & strXMLPath & """>" & strXMLPath & "</a>"

      'create a new XPathDocument object from the XmlTextReader
      Dim objXPDoc As New XPathDocument(objXTReader)

      'create a new XPathNavigator object from the XPathDocument
      Dim objXPNav As XPathNavigator
      objXPNav = objXPDoc.CreateNavigator()

      'create a variable to hold the XmlReader object that is
      'returned from the Transform method
      Dim objReader As XmlReader

      'perform the transformation using the XSL file in the
      'XSLTransform and the XML document referenced by the
      'XPathNavigator. The result is in the XmlReader object
      objReader = objTransform.Transform(objXPNav, Nothing)

      'display the contents of the XmlReader object
      objReader.MoveToContent()
      outResults.InnerText = objReader.ReadOuterXml()

      'create an XMLTextWriter object to write result to disk
      Dim objWriter As New XmlTextWriter(strOutPath, Nothing)

      'write the opening <?xml .. ?> declaration and a comment
      objWriter.WriteStartDocument()
      objWriter.WriteComment("List of authors created " & Now())

      'transform the XML into the XMLTextWriter
      objTransform.Transform(objXPNav, Nothing, objWriter)

      'ensure that all open elements are closed to end the document
      objWriter.WriteEndDocument()

      'flush the buffer to disk and close the file
      objWriter.Close()
      outFile.InnerHtml = "<a href=""" & strOutPath & """>" & strOutPath & "</a>"

   '------------ error handling code -------------
   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

End Sub

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
